package com.daisuxia.risk.service;

import java.util.HashMap;


import java.util.LinkedHashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
import com.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthqueryRequest;
import com.antgroup.zmxy.openplatform.api.response.ZhimaAuthInfoAuthqueryResponse;
import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.back.utils.SysCacheUtils;
import com.daisuxia.back.utils.WebClient;
import com.daisuxia.risk.pojo.RiskCreditUser;
import com.daisuxia.risk.pojo.RiskOrders;
import com.daisuxia.risk.utils.ConstantRisk;
import com.daisuxia.web.pojo.BackConfigParams;
import com.daisuxia.web.util.OrderNoUtil;
/**
 * 白骑士服务
 * @author zhangsh Tel:13817876783
 * @date 2017年10月18日 上午9:12:53
 * @Title: BqsService
 * @ClassName: BqsService
 * @Description:
 */
@Service
public class BqsService implements IBqsService {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private IRiskOrdersService riskOrdersService;

	@Override
	public ServiceResult getRisk(HashMap<String, Object> params) {
		ServiceResult serviceResult = new ServiceResult("500", "未知异常，请稍后重试！");
		try {
			Object userName = params.get("userName");
			Object cardNum = params.get("cardNum");
			Object userId = params.get("userId");
			Object userPhone = params.get("userPhone");
			if (userName != null && cardNum != null && userId != null
					&& userPhone != null) {
				LinkedHashMap<String, String> map2 = SysCacheUtils
						.getConfigParams(BackConfigParams.BQS);
				String url = map2.get("BQS_URL");
				String jsonStrData = "{\"partnerId\":\""
						+ map2.get("BQS_PARENT_ID") + "\",\"verifyKey\":\""
						+ map2.get("BQS_VERIFY_KEY") + "\",\"appId\":\""
						+ map2.get("BQS_APP_ID") + "\",\"eventType\":\""
						+ map2.get("BQS_EVENT_TYPE") + "\",\"certNo\":\""
						+ cardNum + "\",\"mobile\":\"" + userPhone
						+ "\",\"name\":\"" + userName + "\"}";
				RiskOrders orders = new RiskOrders();
				orders.setUserId(userId + "");
				orders.setOrderType(ConstantRisk.BQS);
				orders.setAct(ConstantRisk.BQS_RISK);
				orders.setOrderNo(OrderNoUtil.getInstance().getUUID());
				orders.setReqParams("url=" + url + ",jsonStrData="
						+ jsonStrData);
				logger.info("bqs card"+cardNum);
//				logger.info("getRisk send  " + orders.toString());
				riskOrdersService.insert(orders);
				String result = WebClient.getInstance().postJsonData(url,
						jsonStrData,null);
//				logger.info("getRisk return " + result);
				if (StringUtils.isNotBlank(result)) {
					JSONObject jsonObject = JSONObject.fromObject(result);
					if ("BQS000".equals(jsonObject.getString("resultCode"))) {
						serviceResult = new ServiceResult(
								ServiceResult.SUCCESS, result);
						orders.setStatus(RiskOrders.STATUS_SUC);
					} else {
						serviceResult = new ServiceResult("300", jsonObject
								.getString("resultCode")
								+ jsonObject.getString("resultDesc"));
					}
					orders.setReturnParams(result);
					riskOrdersService.update(orders);
				} else {
					orders.setReturnParams("td return null");
					serviceResult = new ServiceResult("100", "同盾返回空或请求报错");
				}
				riskOrdersService.update(orders);

			} else {
				serviceResult = new ServiceResult("400", "必要参数不足！");
			}
		} catch (Exception e) {
			logger.error("getRisk error ,params=" + params, e);
		}
		return serviceResult;
	}
}
